package com.yyfax.ph.common.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.ph.R;

/**
 * 圆点类.
 * Created by ying_he on 2017/8/3.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class RedDotView extends View {
    private Paint mPaint;
    private int mDotColor = -1;
    private float mDotRadius = 2;
    public RedDotView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, @Nullable AttributeSet attrs){
        mPaint = new Paint();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RedDotView);
        mDotColor = typedArray.getColor(R.styleable.RedDotView_dot_color, Color.RED);
        mDotRadius = typedArray.getDimension(R.styleable.RedDotView_dot_radius, DensityUtil.dip2px(context, 2));
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = (int)Math.ceil(mDotRadius * 2);
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = (int)Math.ceil(mDotRadius * 2);
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setColor(mDotColor);

        canvas.drawCircle(mDotRadius, mDotRadius, mDotRadius, mPaint);
    }

    public void setRadius(float radius){
        mDotRadius = radius;
        invalidate();
    }

    public void setColor(int color){
        mDotColor = color;
        invalidate();
    }
}
